package com.sky.controller.admin;


import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.admin.DishService;
import com.sky.vo.DishVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags = "菜品相关接口")
@Slf4j
@RequestMapping("/admin/dish")
@RestController()
public class DishController {

    @Autowired
    private DishService dishService;

    /**
     * 菜品分页查询
     * @param dishPageQueryDTO
     * @return
     */
    @ApiOperation("菜品分页查询")
    @GetMapping("/page")
    public Result<PageResult> selectPage(DishPageQueryDTO dishPageQueryDTO){
         PageResult pageResult  =  dishService.selectPage(dishPageQueryDTO);
         return Result.success(pageResult);

    }

    /**
     * 新增菜品
     * @param dishDTO
     * @return
     */
    @CacheEvict(cacheNames = "dish", allEntries = true)
    @ApiOperation("新增菜品")
    @PostMapping
    public Result insert(@RequestBody DishDTO dishDTO){
        dishService.insert(dishDTO);
        return Result.success();
    }

    /**
     * 批量删除菜品
     * @param ids
     * @return
     */
    @CacheEvict(cacheNames = "dish", allEntries = true)
    @ApiOperation("批量删除菜品")
    @DeleteMapping
    public Result deleteBatch(@RequestParam List<Long> ids){
        log.info("菜单批量删除{}",ids);
        // todo
        dishService.deleteBatch(ids);
        return Result.success();
    }

    /**
     * 根据分类id查询菜品
     * @param categoryId
     * @return
     */
    @ApiOperation("根据分类id查询菜品")
    @GetMapping("/list")
    public Result selectList(Long categoryId){
        List<DishVO> selectList = dishService.selectList(categoryId);
        return Result.success(selectList);
    }

    /**
     * 菜品回显
     * @param id
     * @return
     */
    @ApiOperation("根据id查询菜品")
    @GetMapping("/{id}")
    public Result<DishVO> selectById(@PathVariable Long id){
        DishVO dishVO = dishService.selectById(id);
        return Result.success(dishVO);
    }


    /**
     * 修改菜品
     * @param dishDTO
     * @return
     */
    @CacheEvict(cacheNames = "dish", allEntries = true)
    @ApiOperation("修改菜品")
    @PutMapping
    public Result update(@RequestBody DishDTO dishDTO){
        dishService.update(dishDTO);
        return Result.success();
    }

    /**
     * 菜品起售、停售
     * @param status
     * @param id
     * @return
     */
    @CacheEvict(cacheNames = "dish", allEntries = true)
    @ApiOperation("菜品起售、停售")
    @PostMapping("/status/{status}")
    public Result status(@PathVariable Integer status, Long id){
        dishService.status(status, id);
        return Result.success();
    }





}
